package com.scu.bank.util;

import com.scu.bank.model.pojo.UserInfo;

import javax.servlet.http.HttpServletRequest;

public class WebUtil {
    public static ThreadLocal<HttpServletRequest> requestThreadLocal = new ThreadLocal<>();
    public static ThreadLocal<UserInfo> userInfoThreadLocal = new ThreadLocal<>();

    public static void setRequest(HttpServletRequest request){
        requestThreadLocal.set(request);
    }

    public static void setCurrentUser(UserInfo userInfo){
        userInfoThreadLocal.set(userInfo);
    }

    public static HttpServletRequest getRequest(){
        return requestThreadLocal.get();
    }

    public static UserInfo getCurrentUser(){
        return userInfoThreadLocal.get();
    }

    public static String getCurrentUserId(){
        UserInfo userInfo = userInfoThreadLocal.get();
        return userInfo != null ? userInfo.getId() : "0";
    }
}
